--module Src.Main  where
module Main  where

import System
import System.Console.GetOpt


import HaCWB.Parser.Parser
import HaCWB.Parser.Lexer
import HaCWB.Lib.Data
import HaCWB.Lib.EvaluateOneStep


options :: [OptDescr String]
options = [ Option ['h','H']
                   ["help"]
                   (NoArg "H")
                   "output a brief help message"
          , Option ['T','t']
                   ["type"]
                   (ReqArg ('t':) "typeEval")
                   "Type of evaluation (ccs/pi)"
          , Option ['P','p']
                   ["process"]
                   (ReqArg ('p':) "procName")
                   "Process Name"
          ]

main = 
    do args <- getArgs
       let (o,n,errs) = getOpt Permute options args
       let opt  = map (take 1) o
       let arg  = map (drop 1) o
       let opts = zip opt arg
       let errs' = takeUnrecOpts errs
       if ((errs /= []) || ("H" `elem` opt))
         then showErrs errs' options
         else processOptions opt opts 
    where 
      takeUnrecOpts [] = []
      takeUnrecOpts (x:xs) = let x' = drop 22 x
                             in (take 1 x') : takeUnrecOpts xs


--------------------------------------------------------------------------------
--	Se houver erros na chamada da ferramenta, eles são colocados um por 
--	linha, seguidos do menú de ajuda.
--------------------------------------------------------------------------------

showErrs [] opts = do showErrs' [] opts
showErrs l opts = do putStr "\nUnrecognized options: "
                     showErrs' l opts


showErrs' [] opts = putStr ((usageInfo usageheader opts) ++ 
               "\n\nFor more information see README file.\n\n")
  where
    usageheader = "\n\nHaCWB - A Haskell tool to manipulate processes.\n" ++ 
                  "Usage: hacwb options ... \n\nList of options:\n"
showErrs' [x] opts = do putStr x
                        showErrs' [] opts
showErrs' (x:xs) opts = do putStr (x ++ ", ")
                           showErrs' xs opts

--------------------------------------------------------------------------------
--	Processamento da lista de ooções introduzida pelo utilizador.
--------------------------------------------------------------------------------


processOptions :: [String]            --Options list
               -> [(String,String)]   --Pair of option - argument
               -> IO()

processOptions opts l = let type_eval = if("t" `elem` opts)
                                        then let t = snd((head . filter ((=="t") . fst)) l)
                                             in if(t `elem` ["pi", "ccs"])
                                                then t
                                                else "pi"
                                        else "pi"
                        in case("p" `elem` opts) of 
                             False -> putStr("Process name missing" ++ (show l) ++ "\n")
                             True  -> let nameP = snd((head . filter ((=="p") . fst)) l)
                                      in evaluate nameP type_eval
    where 
      evaluate nameP t =
          do cont <- getContents
             let lstProcs = parse cont
--             let lstProcs = (parse . alexScanTokens) cont
             eval lstProcs t nameP
      eval (Left a) _ _ = putStr (a++"\n");
      eval (Right lstProcs) t nameP = 
          let proc = getP lstProcs nameP
          in if(t == "ccs")
             then print (eval1stepccs lstProcs proc)
             else print (eval1stepPi lstProcs proc)
      getP lsp n = snd(head(filter ((== n) . fst) lsp))


{-

do cont <- getContents
          let lstProcs = (reconhecedor . alexScanTokens) cont
          print (eval1stepPi lstProcs (getP lstProcs "TPI22")) 
    where 
      getP lsp n = snd(head(filter ((== n) . fst) lsp))
-}
